package com.zsgeek.wisdchain.qutu.view.home;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.base.http.JsonRequest;
import com.zsgeek.wisdchain.qutu.base.http.JsonRequestCallBack;
import com.zsgeek.wisdchain.qutu.base.model.QueryUserAccountModel;
import com.zsgeek.wisdchain.qutu.base.model.StringModel;
import com.zsgeek.wisdchain.qutu.base.presenter.SendBalancePresenter;
import com.zsgeek.wisdchain.qutu.tools.BigDecimalUtils;
import com.zsgeek.wisdchain.qutu.tools.ImageUtils;
import com.zsgeek.wisdchain.qutu.tools.TextUtils;
import com.zsgeek.wisdchain.qutu.tools.ToastHelper;
import com.zsgeek.wisdchain.qutu.tools.UrlPathHelper;
import com.zsgeek.wisdchain.qutu.view.BaseFragment;
import com.zsgeek.wisdchain.qutu.view.MainActivity;

import java.math.BigDecimal;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 赚赚
 * <p/>
 */
public class EarnEarningFragment extends BaseFragment {
    @BindView(R.id.nick_name_tv)
    TextView nick_name_tv;
    @BindView(R.id.address_tv)
    TextView address_tv;
    @BindView(R.id.avatar_iv)
    ImageView avatar_iv;
    @BindView(R.id.wdhb_tv)
    TextView wdhb_tv;
    @BindView(R.id.sdhb_tv)
    TextView sdhb_tv;
    @BindView(R.id.zchb_tv)
    TextView zchb_tv;
    @BindView(R.id.ktbsl_tv)
    TextView ktbsl_tv;

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_earn_earning;
    }

    @Override
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    protected void initData() {
        queryUserAccount(MainActivity.address);
        getBalance(MainActivity.address);
//        if (!TextUtils.isEmpty(MainActivity.address)) {
//            if (MainActivity.userDataBean == null) {
//                getQueryUser(MainActivity.address);
//            } else {
//                setView();
//            }
//        }
    }
//
//    @OnClick({R.id.search_layout})
//    void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.search_layout:
//                SearchShowDialog();
//                break;
//        }
//    }

    private void setView(QueryUserAccountModel.DataBean dataModelData) {
        wdhb_tv.setText(dataModelData.getShenyuSum());
        sdhb_tv.setText(dataModelData.getLinquSum());
        zchb_tv.setText(dataModelData.getFachuSum());
        QueryUserAccountModel.DataBean.UserBean user = dataModelData.getUser();
        if (user == null)
            return;
//        QueryUserModel.DataBean.UserBean userBean = MainActivity.userDataBean.getUser();
//        if (userBean != null) {
        nick_name_tv.setText(user.getNickName());
        address_tv.setText(user.getAddr());

        ImageUtils.loadImage(getActivity(), user.getHeadImg(), avatar_iv);
//        }
    }

//    /**
//     * 获取个人资料
//     *
//     * @param address
//     */
//    private void getQueryUser(String address) {
//        new JsonRequest(new JsonRequestCallBack() {
//            @Override
//            public void showResult(Object object, String taskId) {
//                if (object instanceof QueryUserModel) {
//                    QueryUserModel dataModel = (QueryUserModel) object;
//                    if (dataModel.getCode() == 2000) {
//                        MainActivity.userDataBean = dataModel.getData();
//                        setView();
//                    } else ToastHelper.showToast(dataModel.getMessage());
//                }
//            }
//
//            @Override
//            public void showError(String errorMsg, String taskId) {
//                ToastHelper.showToast(errorMsg);
//            }
//        }).postJson(UrlPathHelper.getAdver() + "user/queryUser?addr=" + address, true, null, QueryUserModel.class);
//    }

    /**
     * 红包收益
     *
     * @param address
     */
    private void queryUserAccount(String address) {
        new JsonRequest(new JsonRequestCallBack() {
            @Override
            public void showResult(Object object, String taskId) {
                if (object instanceof QueryUserAccountModel) {
                    QueryUserAccountModel dataModel = (QueryUserAccountModel) object;
                    if (dataModel.getCode() == 2000) {
                        QueryUserAccountModel.DataBean dataModelData = dataModel.getData();
                        setView(dataModelData);
                    } else ToastHelper.showToast(dataModel.getMessage());
                }
            }

            @Override
            public void showError(String errorMsg, String taskId) {
                ToastHelper.showToast(errorMsg);
            }
        }).postJson(UrlPathHelper.getAdver() + "user/queryUserAccount?addr=" + address, true, null, QueryUserAccountModel.class);
    }

    /*
     *获取用户余额
     * pubkeyhash 为用户公钥 hash
     * 拿到的结果，要除以一个亿
     */
    private void getBalance(String address) {
        new SendBalancePresenter(address, new JsonRequestCallBack() {
            @Override
            public void showResult(Object object, String taskId) {
                if (object instanceof StringModel) {
                    StringModel stringModel = (StringModel) object;
                    if (stringModel.getCode() == 2000) {
                        String amount = BigDecimalUtils.bigDecimalToString(new BigDecimal(String.valueOf(stringModel.getData())).divide(new BigDecimal("100000000")));
                        if (TextUtils.isEmpty(amount))
                            amount = "0.000000000";
                        ktbsl_tv.setText(amount);
                    } else ToastHelper.showToast(stringModel.getMessage());
                }
            }

            @Override
            public void showError(String errorMsg, String taskId) {
                ToastHelper.showToast(errorMsg);
            }
        });
    }

    @OnClick({R.id.reading_income_ll, R.id.like_proceeds_ll, R.id.evaluation_earnings_ll, R.id.activity_income_ll, R.id.dynamic_income_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.reading_income_ll:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.ydsy))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
            case R.id.like_proceeds_ll:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.dzsy))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
            case R.id.evaluation_earnings_ll:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.pjsy))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
            case R.id.activity_income_ll:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.hydsy))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
            case R.id.dynamic_income_ll:
                startActivity(new Intent(getActivity(), BaseWebViewActivity.class).putExtra("title", getString(R.string.dtsy))
                        .putExtra("path", "https://game.gamec.biz/paopaolong/regs.htm?coinAddress=" + MainActivity.address));
                break;
        }
    }
}
